import { login, autoLogin, getInfo } from '@/api/login'
import { getToken, setToken, removeToken, setAid, getAid, removeAid } from '@/utils/auth'

const user = {
    state: {
        token: getToken(),
        user: {},
        roles: [],
        // 第一次加载菜单时用到
        loadMenus: false
    },

    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_USER: (state, user) => {
            state.user = user
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
        SET_LOAD_MENUS: (state, loadMenus) => {
            state.loadMenus = loadMenus
        }
    },

    actions: {
        // 登录
        Login({ commit }, userInfo) {
            const rememberMe = userInfo.rememberMe
            return new Promise((resolve, reject) => {
                login(userInfo.up).then(res => {
                    if (res && res.errorCode === 0) {
                        setToken(res.content.token, rememberMe)
                        commit('SET_TOKEN', res.content.token)
                        setAid(res.content.adminInfo.aid, rememberMe)
                        setUserInfo(res.content.adminInfo, commit)
                        // 第一次加载菜单时用到， 具体见 src 目录下的 permission.js
                        commit('SET_LOAD_MENUS', true)
                        resolve()
                    } else {
                        reject(res)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 自动登录
        AutoLogin({ commit }, params) {
            return new Promise((resolve, reject) => {
                autoLogin(params).then(res => {
                    if (res && res.errorCode === 0) {
                        setToken(res.data.token)
                        commit('SET_TOKEN', res.data.token)
                        setAid(res.data.adminInfo.aid)
                        setUserInfo(res.data.adminInfo, commit)
                        // 第一次加载菜单时用到， 具体见 src 目录下的 permission.js
                        commit('SET_LOAD_MENUS', true)
                        resolve()
                    } else {
                        reject(res)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 获取用户信息
        GetInfo({ commit }) {
            return new Promise((resolve, reject) => {
                getInfo(getAid()).then(res => {
                    setUserInfo(res.content, commit)
                    resolve(res.content)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 登出
        LogOut({ commit }) {
            return new Promise((resolve, reject) => {
                // logout().then(res => {
                logOut(commit)
                resolve()
                // }).catch(error => {
                //   logOut(commit)
                //   reject(error)
                // })
            })
        },

        updateLoadMenus({ commit }) {
            return new Promise((resolve, reject) => {
                commit('SET_LOAD_MENUS', false)
            })
        }
    }
}

export const logOut = (commit) => {
    commit('SET_TOKEN', '')
    commit('SET_ROLES', [])
    removeToken()
    removeAid()
}

export const setUserInfo = (res, commit) => {
    // 如果没有任何权限，则赋予一个默认的权限，避免请求死循环
    if (res.roles.length === 0) {
        commit('SET_ROLES', ['ROLE_SYSTEM_DEFAULT'])
    } else {
        commit('SET_ROLES', res.roles)
    }
    commit('SET_USER', res)
}

export default user
